﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace Interface.Houses
{
    public partial class HouseDetail : System.Web.UI.Page
    {
        DataConnection dc = new DataConnection();
        DataValid dv = new DataValid();
        string stringDetail;

        protected void Page_Load(object sender, EventArgs e)
        {
            getQueryString();
        }

        private void getQueryString()
        {
            if (Request.QueryString["ID"] != null)
            {
                stringDetail = Request.QueryString["ID"].ToString();
                DetailBind(stringDetail);
            }
        }

        private void DetailBind(string houseId)
        {
            string sqlStr = "Select * from BoardingHouse where IdHouse='" + houseId + "'";
            DataTable dt = dc.getData(sqlStr);
            DataTable dtHouse = dataTableInitiate();

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    //int ID = Convert.ToInt32(row["IdDistrict"].ToString());
                    int _userId = Convert.ToInt32(row["Owner"].ToString());

                    string getNamesql = "select Fullname from Users where UserId = '" + _userId + "'";
                    string _userName = dc.getStringValue(getNamesql);

                    int _cityId = Convert.ToInt32(row["City"]);

                    string _cityName = dc.getCityName(_cityId);

                    int _disId = Convert.ToInt32(row["District"]);

                    string _disName = dc.getDisName(_disId);

                    string _street = Convert.ToString(row["Street"]);

                    string _ward = Convert.ToString(row["Ward"]);

                    string _diachi = " Đường: " + _street + ", Phường: " + _ward + ", Quận: " + _disName;

                    int _loainhatro = Convert.ToInt32(row["Category"]);

                    string _tenLoainhatro = dc.getCategoryName(_loainhatro);

                    //int _danhgia = Convert.ToInt32(row["Rating"]);
                    //string _dgia = "";

                    //if (_danhgia > 0)
                    //{
                    //    _dgia = Convert.ToString(_danhgia);

                    //}
                    //else
                    //{
                    //    _dgia = "Chưa đánh giá";
                    //}


                    string _lienhe = Convert.ToString(row["Contact"]);
                    int _soPhong = Convert.ToInt32(row["NoOfRooms"]);
                    int _sophongcontrong = Convert.ToInt32(row["RoomAvailable"]);
                    string _hinh = Convert.ToString(row["ImageSource"]);
                    string _mieuta = Convert.ToString(row["Descript"]);
                    string _lat = row["Lat"].ToString();
                    string _long = row["Long"].ToString();
                    DataRow dtRow = dtHouse.NewRow();
                    //dtRow["ID"] = ID;
                    dtRow["ChuNha"] = _userName;
                    dtRow["Duong"] = _street;
                    dtRow["ThanhPho"] = _cityName;
                    dtRow["Phuong"] = _ward;
                    dtRow["Quan"] = _disName;
                    dtRow["LoaiNhaTro"] = _tenLoainhatro;
                    dtRow["SoPhong"] = _soPhong;
                    dtRow["Sophongcontrong"] = _sophongcontrong;
                    //dtRow["DanhGia"] =  _dgia;
                    dtRow["LienHe"] = _lienhe;
                    dtRow["Hinhanh"] = _hinh;
                    dtRow["Mieuta"] = _mieuta;
                    dtRow["Lat"] = _lat;
                    dtRow["Long"] = _long;

                    dtHouse.Rows.Add(dtRow);
                }
            }



            fview.DataSource = dtHouse;
            fview.DataBind();
        }


        protected DataTable dataTableInitiate()
        {
            DataTable dt = new DataTable();
            DataColumn col = dt.Columns.Add("ID", typeof(int));
            col.AutoIncrement = true;
            col.AutoIncrementStep = 1;
            col.AutoIncrementSeed = 1;

            //dt.Columns.Add("STT", typeof(string));
            dt.Columns.Add("ChuNha", typeof(string));
            dt.Columns.Add("Duong", typeof(string));
            dt.Columns.Add("Phuong", typeof(string));
            dt.Columns.Add("Quan", typeof(string));
            dt.Columns.Add("ThanhPho", typeof(string));
            dt.Columns.Add("LoaiNhaTro", typeof(string));
            //dt.Columns.Add("DanhGia", typeof(string));
            dt.Columns.Add("LienHe", typeof(string));
            dt.Columns.Add("SoPhong", typeof(int));
            dt.Columns.Add("Sophongcontrong", typeof(int));
            dt.Columns.Add("Hinhanh", typeof(string));
            dt.Columns.Add("Mieuta", typeof(string));
            dt.Columns.Add("Lat", typeof(string));
            dt.Columns.Add("Long", typeof(string));
            
            return dt;
        }


        protected void btnEdit_Click(object sender, EventArgs e)
        {
            fview.ChangeMode(FormViewMode.Edit);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("BoardingHouse.aspx");
        }

        protected void fview_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {

        }

        protected void ddlEditCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            string _CityName = ((DropDownList)fview.FindControl("ddlEditCity")).SelectedItem.Text;
            string sqlDis = "select IdCity from City where Name = N'" + _CityName + "'";
            int _IdCity = dc.getNumberValue(sqlDis);
            string sqlStr = "Select * from District where IdCity='" + _IdCity + "'";
            DataTable dt = dc.getData(sqlStr);
            DropDownList ddDis = ((DropDownList)fview.FindControl("ddlEditDis"));
            if (dt.Rows.Count > 0)
            {
                ddDis.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string _Name = dt.Rows[i]["Name"].ToString();
                    ddDis.Items.Add(_Name);
                }
            }
            else
            {
                ddDis.Items.Clear();
                string Error = "Mời Bạn Chọn Thành Phố Khác";
                ddDis.Items.Add(Error);
                lblError.Text = "Thành Phố Bạn Chọn Chưa Có Nhà Và Phòng Cho Thuê";
 
            }
            
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(stringDetail);
            string _fullName, _thanPho, _quan, _duong, _phuong, _dienthoai, _soPhong, _soPhongCon, _mieuTa, _loai, _lat, _long;
            _fullName = ((TextBox)fview.FindControl("txtEditName")).Text;
            _thanPho = ((DropDownList)fview.FindControl("ddlEditCity")).Text;

            string sqlIdThanhPho = "select IdCity from City where Name =N'" + _thanPho + "'";
            int _idThanhPho = dc.getNumberValue(sqlIdThanhPho);


            _quan = ((DropDownList)fview.FindControl("ddlEditDis")).SelectedValue.ToString();
            string sqlIdQuan = "select IdDistrict from District where Name =N'" + _quan + "'";
            int _idQuan = dc.getNumberValue(sqlIdQuan);


            _loai = ((DropDownList)fview.FindControl("ddlEditLoaiNhaTro")).SelectedValue.ToString();
            string sqlIdLoai = "select IdCategory from Category where Name =N'" + _loai + "'";
            int _Idloai = dc.getNumberValue(sqlIdLoai);


            _duong = ((TextBox)fview.FindControl("txtDuong")).Text;

            _phuong = ((TextBox)fview.FindControl("txtPhuong")).Text;

            _dienthoai = ((TextBox)fview.FindControl("txtDienThoai")).Text;

            _soPhong = ((TextBox)fview.FindControl("txtSoPhong")).Text;



            _soPhongCon = ((TextBox)fview.FindControl("txtPhongCon")).Text;

            _mieuTa = ((TextBox)fview.FindControl("txtEditMieuTa")).Text;

            _lat = ((TextBox)fview.FindControl("txtLat")).Text;

            bool checkedLat = dv.Isnumeric(_lat);
            string lat = null;
            if (checkedLat == true)
            {
                lat = Convert.ToString(_lat);
            }

            else
            {
                lblError.Text = "Bạn phải nhập vào số";
                return;
            }

            _long = ((TextBox)fview.FindControl("txtLong")).Text;

            bool checkedLong = dv.Isnumeric(_long);
            string stringlong = null;
            if (checkedLong == true)
            {
                stringlong = Convert.ToString(_long);
            }

            else
            {
                lblError.Text = "Bạn phải nhập vào số";
                return;
            }




            bool _checkedSoPhong = dv.isInt(_soPhong);
            bool _checkedSoPhongCon = dv.isInt(_soPhongCon);
            bool _checkedDt = dv.isInt(_dienthoai);
             //&&  && _checkedDt == true && )
            if (_checkedSoPhong == false)
            {
                lblError.Text = "Bạn phải nhập vào số phòng!";
                return;

            }
            else if(_checkedSoPhongCon == false)
            {
                lblError.Text = "Bạn phải nhập số phòng còn lại!";
                 return;
            }
            else if(_checkedDt == false)
            {
                 lblError.Text = "Bạn phải nhập số điện thoại!";
                 return;
            }

            else if (Convert.ToInt32(_soPhong) < Convert.ToInt32(_soPhongCon))
            {
                lblError.Text = "Số phòng còn trống phải ít hơn số phòng của nhà trọ!";
                return;
            }
            else
            {
                string sqlIdUser = "select Owner from BoardingHouse where IdHouse='"+ id +"'";
                int _idUser = dc.getNumberValue(sqlIdUser);

                string sqlUpdateFullName = "update Users set Fullname = N'" + _fullName + "' where UserId = '" + _idUser + "'";
                dc.ExecNonQuery(sqlUpdateFullName);

                string sqlUdateHouse = "update BoardingHouse set Street = N'" + _duong + "', Ward = N'" + _phuong + "', District = '" + _idQuan + "', City = '" + _idThanhPho + "', Category = '" + _Idloai + "', Contact = '" + _dienthoai + "', NoOfRooms = '" + _soPhong + "', RoomAvailable = '" + _soPhongCon + "', Lat = '" + lat + "', Long='" + stringlong +"'  where IdHouse = '" + id + "'";
                dc.ExecNonQuery(sqlUdateHouse);
                Response.Redirect("HouseDetail.aspx?ID=" + id);
            }

            
        }

        protected void btnChiTiet_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(stringDetail);
            Response.Redirect("~/Rooms/RoomList.aspx?ID="+ id);
        }


        

    }
}